const { OrdersModel } = require('./models/ordersModel');
const { tenantsModel } = require('./models/tenantsModel');
const { parkingsModel } = require('./models/parkingsModel');
require('./models/parkingsModel')
require('./models/lessorsModel')
require('./models/tenantsModel')
//获取订单
module.exports.getOrders = async function (msg) {
    if (msg.state) {
        var total = (await (await OrdersModel.find({ isDelete: false } && { state: msg.state }))).length
        var data = await OrdersModel.find({ isDelete: false } && { state: msg.state }).populate("parkingsId").populate("tenantsId").populate("lessorsId")
            .limit(msg.pageSize - 0).skip((msg.currentPage - 1) * msg.pageSize);
        var pages = Math.ceil(total / msg.pageSize)
    } else {
        var total = (await (await OrdersModel.find({ isDelete: false }))).length
        var data = await OrdersModel.find({ isDelete: false }).populate("parkingsId").populate("tenantsId").populate("lessorsId")
            .limit(msg.pageSize - 0).skip((msg.currentPage - 1) * msg.pageSize);
        var pages = Math.ceil(total / msg.pageSize)
    }
    return {
        data, total
    }

}
//添加订单
module.exports.addOrders = async function (data) {
    return await OrdersModel.create(data)
}
//删除订单(软删除,修改isDelete属性为false)
module.exports.delOrders = async function (data) {

    return await OrdersModel.updateOne(data, { isDelete: true })  //传入对象{}
}
//查找订单
module.exports.findOrders = async function (msg) {
    const total = (await OrdersModel.find({[msg.type]:{$regex:msg.value,$options:'i'}})).length
    var data = await OrdersModel.find({[msg.type]:{$regex:msg.value,$options:'i'}}).populate("parkingsId").populate("tenantsId").populate("lessorsId")
        .limit(msg.pageSize - 0).skip((msg.currentPage - 1) * msg.pageSize);
    console.log(data)
    return {
        data,
        total,
    }

}